package com.ai.zhly.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.base.sequence.SequenceGenerator;
import com.ai.dmc.index.common.InitParam;
import com.ai.dmc.index.common.bean.AnalysisBean;
import com.ai.dmc.index.common.bean.AreaBean;
import com.ai.dmc.index.common.bean.CommonBaseBean;
import com.ai.dmc.index.common.bean.ExportBean;
import com.ai.dmc.index.common.cache.AreaCache;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.index.common.util.BasicKMeans;
import com.ai.dmc.index.common.util.DateUtil;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @Title: MapDataImpl.java
 * @Description: 家装宽带展示层service
 * @author xueyf3
 * @date 2016年5月23日 下午4:22:54 Copyright：Copyright (c) 2016
 *       Company：北京亚信智慧数据科技有限公司
 * @version V1.0
 */
@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
public class LocalAnalysisServiceImpl extends BaseServiceImpl implements ILocalAnalysisService {
	// 定义对应Mapper文件的命名空间
	private static final String NAMESPACE = "LocalAnalysis.";

	public static Logger log = LoggerFactory.getServiceLog(LocalAnalysisServiceImpl.class);

	private SequenceGenerator sequenceGenerator;

	private ICommonService commonService;

	public void setSequenceGenerator(SequenceGenerator sequenceGenerator) {
		this.sequenceGenerator = sequenceGenerator;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	@Override
	public void querySourceCityList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		
		List<CommonBaseBean> rList = new ArrayList<CommonBaseBean>();
		try {
			rList = this.getBaseDao().queryForList(
					NAMESPACE + "querySourceCityList",inputObject.getParams(),
					CommonBaseBean.class);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		List<JSONObject> rl = new ArrayList<JSONObject>();
		for (int i = 0; i < rList.size(); i++) {
			String city_id = rList.get(i).getQuery_value1();
			String city_name = rList.get(i).getQuery_value2();
			JSONObject tmpObj = new JSONObject();
			tmpObj.put("city_id", city_id);
			tmpObj.put("city_name", city_name);
			rl.add(tmpObj);
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("cityList", null == rl ? "" : JsonUtil.Object2Json(rl));
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
	
	}
	
	public void queryBarChartsList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		List<CommonBaseBean> rList = new ArrayList<CommonBaseBean>();
		Map params=new HashMap();
		String srcCity=inputObject.getParams().get("srcCity");
		String descCity=inputObject.getParams().get("descCity");
		String start=inputObject.getParams().get("start");
		String end=inputObject.getParams().get("end");
		String ctg=inputObject.getParams().get("ctg");
		params.put("srcCity",srcCity);
		params.put("descCity",descCity);
		params.put("start",start);
		params.put("end",end);
		params.put("ctg",ctg);
		if(ctg.equals("d")){
		   try {
			   rList = this.getBaseDao().queryForList(
					NAMESPACE + "queryBarChartsList_d",params,
					CommonBaseBean.class);
		   } catch (Exception e) {
			        System.out.println(e.getMessage());
		     }
		}else{
			try {
				rList = this.getBaseDao().queryForList(
						NAMESPACE + "queryBarChartsList_m",params,
						CommonBaseBean.class);
			  } catch (Exception e) {
				System.out.println(e.getMessage());
			  }
		}
		List axisNames = new ArrayList();
		List axisValues = new ArrayList();
		for (int i = 0; i < rList.size()&&i<10; i++) {
			String axisName = rList.get(i).getQuery_value1();
			int axisValue = Integer.parseInt(rList.get(i).getQuery_value2());
			double db = (double) axisValue/10000;
			double divAxisValue = new BigDecimal(db)
					.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue(); // 四舍五入，保留1位小数
			axisNames.add(axisName);
			axisValues.add(divAxisValue);
		}
		Map localAnalysisMap = new HashMap();
		localAnalysisMap.put("axisNames", axisNames);
		localAnalysisMap.put("axisValues", axisValues);
		JSONObject localAnalysisJson = JSONObject.fromObject(localAnalysisMap);		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("localAnalysisData", null == localAnalysisJson ? "" : JsonUtil.Object2Json(localAnalysisJson));
		outputObject.setObject(JsonUtil.Object2Json(resultMap));		
	}

	
	//下载
	public void dlTrendChartsList(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		List<CommonBaseBean> rList = new ArrayList<CommonBaseBean>();
		Map params=new HashMap();
		String[] srcCity=inputObject.getParams().get("srcCity").split(":");
		String[] descCity=inputObject.getParams().get("descCity").split(":");
		String start=inputObject.getParams().get("start");
		String end=inputObject.getParams().get("end");
		String ctg=inputObject.getParams().get("ctg");
		params.put("srcCity",srcCity[0]);
		params.put("descCity",descCity[0]);
		params.put("start",start);
		params.put("end",end);
		params.put("ctg",ctg);
		if("d".equals(ctg)){
		   try {
			   rList = this.getBaseDao().queryForList(
					NAMESPACE + "queryBarChartsList_d",params,
					CommonBaseBean.class);
		   } catch (Exception e) {
			        System.out.println(e.getMessage());
		     }
		}else{
			try {
				rList = this.getBaseDao().queryForList(
						NAMESPACE + "queryBarChartsList_m",params,
						CommonBaseBean.class);
			  } catch (Exception e) {
				System.out.println(e.getMessage());
			  }
		}
		
		List dlValues = new ArrayList();
		for (CommonBaseBean cm :rList) {
			Map map=new HashMap();		
			String sceneName = cm.getQuery_value1();
			int peopCnt = Integer.parseInt(cm.getQuery_value2());
			double db = (double) peopCnt/10000;
			double divPeopCnt = new BigDecimal(db)
					.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue(); // 四舍五入，保留1位小数
             map.put("sceneName", sceneName);
             map.put("divPeopCnt", String.valueOf(divPeopCnt));
             dlValues.add(map);
		}
		String rowNum = "2";
		String title = "旅游景区::客流人数(万)";
		String fileName ="游客分布"+"_"+start+"~"+end+"_"+srcCity[1]+"至"+descCity[1]; //createDownloadFileName("游客分布", params);
		String sheetName = "游客分布";
		String expType = "1";

		ExportBean export = new ExportBean(rowNum, title, fileName, sheetName, expType, dlValues);
//		JSONObject json = JSONObject.fromObject(export);
		outputObject.setObject(export);		
	}
}
